// pages/wattingPayment/wattingPayment.js
var app = getApp();
Page({

    /**
     * 页面的初始数据
     */
    data: {
        no_data_tips:true
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {

    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        this.get_storage();
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {
        wx.switchTab({
            url: '../../pages/myPage/myPage',
        })
    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    },
    get_storage: function () {
        var that = this;
        wx.getStorage({
            key: 'memberInfo',
            success: function (res) {
                var data = res.data;
                var member_id = data.id;
                var open_id = data.open_id;
                that.setData({
                    member_id: member_id,
                    open_id: open_id
                })
                that.order_list(member_id)
            }
        })
    },
    order_list: function (member_id) {
        var that = this;
        var params = {};
        params['id'] = member_id;
        params['mall_status'] = 1;
        wx.request({
            url: app.config() + "member/memberOrder",
            data: params,
            header: {
                'content-type': 'application/json'
            },
            success: function (res) {
                var data = res.data.data;
                var len=data.length;
                if(len){
                    for (var i = 0; i < len; i++) {
                        data[i].product_number = data[i].product_order.length
                    }
                }else{
                    that.setData({
                        no_data_tips: false
                    })
                }
                that.setData({
                    order_list: data
                })
            }
        })
    },//支付
    pay_ment: function (e) {
        wx.showLoading({
            title: '加载中',
        })
        var that = this;
        var order_id = e.currentTarget.dataset.order_id;
        var open_id = that.data.open_id;
        var params = {};
        params['order_id'] = order_id;
        params['open_id'] = open_id;
        wx.request({
            url: app.config() + "order/xiaowxpay",
            data: params,
            header: {
                'content-type': 'application/json'
            },
            success: function (res) {
                wx.hideLoading();
                if(res.data.status==0){
                    var data = res.data.data;
                    var timeStamp = data.timeStamp;//时间戳从1970年1月1日00:00:00至今的秒数,即当前的时间
                    var nonceStr = data.nonceStr;//随机字符串，长度为32个字符以下
                    var packages = data.package;
                    var signType = data.signType;
                    var paySign = data.paySign;
                    wx.requestPayment({
                        'timeStamp': timeStamp,
                        'nonceStr': nonceStr,
                        'package': packages,
                        'signType': 'MD5',
                        'paySign': paySign,
                        'success': function (res) {
                            that.success_pay(order_id)
                        },
                        'fail': function (res) {
                            
                        },
                        'complete':function(res){
                            if (res.errMsg ==="requestPayment:cancel"){
                                wx.showToast({
                                    title: '支付失败',
                                    image: "../../images/warning.png"
                                })
                            }
                        }
                    })
                }else{
                    wx.showToast({
                        title: res.data.message,
                        image:'../../images/warning.png'
                    })
                }
                
            }
        })
    },//支付成功
    success_pay: function (order_id) {
        var params = {};
        params['order_id'] = order_id;
        wx.request({
            url: app.config() + "order/xiaoSuccessPay",
            data: params,
            header: {
                'content-type': 'application/json'
            },
            success: function (res) {
                if (res.data.status == 0) {
                    wx.navigateTo({
                        url: '../../pages/successPay/successPay?order_id=' + order_id,
                    })
                }
            }

        })
    },//取消订单
    cangel_order: function (e) {
        var that = this;
        var member_id = that.data.member_id;
        wx.showModal({
            title: '',
            content: '确认取消该订单？',
            success:function(res){
                if (res.confirm) {
                    var order_id = e.currentTarget.dataset.order_id;
                    var params = {};
                    params['id'] = order_id;
                    wx.request({
                        url: app.config() + "order/cancelOrder",
                        data: params,
                        header: {
                            'content-type': 'application/json'
                        },
                        success: function (res) {
                            var data = res.data;
                            if (data.status == 0) {
                                that.order_list(member_id)
                                wx.showToast({
                                    title: '取消成功',
                                    icon: "success"
                                })
                            }
                        }
                    })
                } else if (res.cancel) {

                }
            }
        })
        
    }
})